package com.xiaoximen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class EditEventActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_event);
		
		init();
	}
	
	private LinearLayout mCurrentEditWrapper, mNextEditWrapper;
	private EditText mCurrentEdit, mNextEdit;
	private LinearLayout mEditListLayout;
	
	private void init() {
		
		findViewById(R.id.edit_event_confirm).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), EditTimeListActivity.class);
				startActivity(intent);
			}
		});
		
		mEditListLayout = (LinearLayout) findViewById(R.id.edit_event_place_list_layout);
		addNewPlaceEdit();
		changeFocusToNextEdit();
	}
	
	private void addNewPlaceEdit() {
		
		LayoutInflater inflater = LayoutInflater.from(this);
		
		mNextEditWrapper = (LinearLayout) inflater.inflate(R.layout.edit_new_place, null);
		mNextEdit = (EditText) mNextEditWrapper.findViewById(R.id.edit_event_place_edit);
		mEditListLayout.addView(mNextEditWrapper);
	}
	
	private void onCompleteOneOption() {
		mCurrentEdit.setBackgroundResource(R.drawable.editedtext);
		mCurrentEdit.setPadding(10, 5, 10, 5);
		mCurrentEdit.setTextSize(20);
		mCurrentEdit.setEnabled(false);
		
		View deleteView = mCurrentEditWrapper.findViewById(R.id.edit_event_place_delete);
		deleteView.setVisibility(View.VISIBLE);
		final LinearLayout wrapper = mCurrentEditWrapper;
		deleteView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mEditListLayout.removeView(wrapper);
			}
		});
		
		if (mCurrentEdit.getText().toString().equals("")) {
			mEditListLayout.removeView(mCurrentEditWrapper);
		}

		addNewPlaceEdit();
		changeFocusToNextEdit();
	}
	
	private void configureCurrentEdit() {

		mCurrentEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus) {
					onCompleteOneOption();
				}
			}
		});
		
		mCurrentEdit.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH
						|| actionId == EditorInfo.IME_ACTION_DONE
						|| event.getAction() == KeyEvent.ACTION_DOWN
						&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
					onCompleteOneOption();;
				}
				return false;
			}
		});
	}
	
	private void changeFocusToNextEdit() {

		mCurrentEdit = mNextEdit;
		mCurrentEditWrapper = mNextEditWrapper;
		mEditListLayout.requestChildFocus(mCurrentEditWrapper, mCurrentEdit);
		configureCurrentEdit();
	}
}
